package com.team.controller;

import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.team.entity.ComputerRoom;
import com.team.entity.Equipment;
import com.team.service.ComputerRoomService;

@Controller
public class ComputerRoomController {
	@Autowired
	private ComputerRoomService crs;
	
	@RequestMapping("selectcomputerroom")
	public String selectComputerRoom(String name,HttpSession session,Model model){
		List<ComputerRoom> cr=crs.findAllComputerRoom();
		session.setAttribute("findAllcr", cr);
//		model.addAttribute("findAllcr", cr);
		if (name.equals("admin")) {
			return "computerroominformation.jsp";
		}else{
			return "roomreservation.jsp";
		}
		
	}
	
	@RequestMapping("deletemachineroom")
	public String deletemachineroom(HttpSession session,Model model){
		List<ComputerRoom> cr=crs.findAllComputerRoom();
		session.setAttribute("findAllcr", cr);
		return "deletemachineroom.jsp";
	}
	
	@RequestMapping("selectcomputerroomid")
	public String selectComputerRoomid(Integer id,Model model){
		ComputerRoom computerRoom = crs.computerRoomById(id);
		model.addAttribute("computerRoom", computerRoom);
		Set<Equipment> equipment = computerRoom.getEquipment();
		int count=0;
		for (int i = 0; i < equipment.size(); i++) {
			count++;
		}
		model.addAttribute("count",count);
//		for (Equipment equipment2 : equipment) {
//			count=equipment2.getId();
//		}
//		System.out.println(count);
		return "computerroomequipment.jsp";
	}
	
	@RequestMapping("savacomputerroom")
	public String savaComputerRoom(ComputerRoom computerRoom,HttpServletRequest request){
		ComputerRoom computerRoom1 = crs.ComputerRoomInformation(computerRoom.getComputerroomid());
		if (computerRoom1==null) {
			ComputerRoom savaComputerRoom = crs.saveComputerRoom(computerRoom);
			if (savaComputerRoom!=null) {
				return "menu_admin.jsp";
			}else {
				return "addcomputerroom.jsp";
			}
		} else {
			request.setAttribute("status", "该机房编号已存在");
			return "addcomputerroom.jsp";
		}
		
	}
	@RequestMapping("deleteComputerRoom")
	public String deleteComputerRoom(Integer id,Model model){
		boolean status = crs.deleteComputerRoom(id);
		if (status) {
			return "menu_admin.jsp";
		}else {
			return "computerroominformation.jsp";
		}
	}
	
	@RequestMapping("updateComputerRoom")
	public String updateComputerRoom(ComputerRoom computerRoom,HttpServletRequest request){
		ComputerRoom computerRoom1 = crs.ComputerRoomInformation(computerRoom.getComputerroomid());
		if (computerRoom1==null) {
			boolean status = crs.updateComputerRoom(computerRoom);
			if (status) {
				return "menu_admin.jsp";
			}else {
				return "updatecomputerroom.jsp";
			}
		} else {
			request.setAttribute("status", "该机房编号已存在");
			return "updatecomputerroom.jsp";
		}
		
	}
	
	@RequestMapping("ComputerRoomInformation")
	public String ComputerRoomInformation(Integer id,Model model){
		ComputerRoom computerRoom = crs.computerRoomById(id);
		model.addAttribute("computerRoom", computerRoom);
		return "updatecomputerroom.jsp";
	}
	
	@RequestMapping("roomReservation")//机房预约
	public String roomReservation(Integer id,String booker, HttpSession session,HttpServletRequest request){
		ComputerRoom computerRoom = crs.computerRoomById(id);
		ComputerRoom myreservation = crs.myReservation(booker);
		if (computerRoom.getPredeterminedstate().equals("可预约")) {
			if (myreservation==null) {
				computerRoom.setBooker(booker);
				computerRoom.setPredeterminedstate("预约成功");
				crs.updateComputerRoom(computerRoom);
				List<ComputerRoom> cr=crs.findAllComputerRoom();
				session.setAttribute("findAllcr", cr);
				return "roomreservation.jsp";
			} else {
				request.setAttribute("status", "您已预约过其他机房，请勿重复预约");
				return "roomreservation.jsp";
			}
			
		}else {
			return "appointmentfailed.jsp";
		}
		
	}
	
	@RequestMapping("cancelreservation")//取消预约
	public String cancelreservation(Integer id,String booker,HttpSession session,HttpServletRequest request){
		ComputerRoom computerRoom = crs.computerRoomById(id);
		if (computerRoom.getPredeterminedstate().equals("可预约")) {
			request.setAttribute("cancelreservation", "未预约，无法取消！");
			return "roomreservation.jsp";
			}else {
				if (computerRoom.getBooker().equals(booker)) {
					computerRoom.setBooker(null);
					computerRoom.setPredeterminedstate("可预约");
					crs.updateComputerRoom(computerRoom);
					List<ComputerRoom> cr=crs.findAllComputerRoom();
					session.setAttribute("findAllcr", cr);
					request.setAttribute("cancelreservation", "已取消预约！");
					return "roomreservation.jsp";
				} else {
					request.setAttribute("status", "不属于您的预约，无法取消");
					return "roomreservation.jsp";
				}
			
		}
	}
	@RequestMapping("myreservation")//我的预约
	public String myreservation(String booker,HttpSession session){
		ComputerRoom myreservation = crs.myReservation(booker);
			session.setAttribute("myreservation", myreservation);
			return "myreservation.jsp";
	}
	
}
